<template>
  <div class="table-section">
    <div class="table-section_tool">
      <el-form class="tool-search" :model="queryParams" ref="queryFormRef" :inline="true">
        <el-form-item prop="settleStatus">
          <DictSelect dict="settle_status" v-model="queryParams.stasettleStatustus" placeholder="结算状态" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <div class="tool-handle">
        <el-button type="primary" icon="Plus" @click="handleAdd">新增电量</el-button>
      </div>
    </div>
    <vxe-grid ref="tableRef" v-bind="gridOptions">
      <template #status="{ row }">
        <dict-tag :options="settle_status" :value="row.settleStatus" />
      </template>
      <template #handle="{ row }">
        <el-button link type="primary" @click="handleDetail(row)" v-hasPermi="['system:tenant:edit']">详情</el-button>
        <el-button link type="primary" :display="row.settleStatus == 1" @click="handleUpdate(row)" v-hasPermi="['system:tenant:edit']"
          >编辑</el-button
        >
        <el-button link type="danger" :display="row.settleStatus == 0" @click="handleDelete(row)" v-hasPermi="['system:tenant:edit']">删除</el-button>
      </template>
      <template #bottom>
        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
      </template>
    </vxe-grid>
    <record-add ref="recordAddRef" @close="handleQuery"></record-add>
    <record-info ref="recordInfoRef" @close="handleQuery"></record-info>
  </div>
</template>

<script setup name="StationEnergyRecord" lang="ts">
import { listEnergyRecord, delEnergyRecord } from '@/api/station/energy/index';
import { StationEnergyRecord, StationEnergyQuery } from '@/api/station/energy/types';
import { getTableConfig } from './columns';
import RecordAdd from '../record-add/index.vue';
import RecordInfo from '../record-info/index.vue';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const props = defineProps({
  station: [String, Number]
});
const { settle_status } = toRefs<any>(proxy?.useDict('settle_status'));
const gridOptions = reactive(getTableConfig());
const data = toRef(gridOptions, 'data');
const loading = toRef(gridOptions, 'loading');

const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const recordAddRef = ref();
const recordInfoRef = ref();

const queryParams = ref<StationEnergyQuery>({
  pageNum: 1,
  pageSize: 20,
  stationName: '',
  status: '',
  regionCode: ''
});

/** 查询租户列表 */
const getList = async () => {
  loading.value = true;
  const res = await listEnergyRecord({ ...queryParams.value, station: props.station });
  data.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 搜索重置操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 新增按钮操作 */
const handleAdd = () => {
  recordAddRef?.value.open(props.station);
};

/** 修改按钮操作 */
const handleUpdate = async (row?: StationEnergyRecord) => {
  recordAddRef?.value.openEdit(row);
};
/** 详情按钮操作 */
const handleDetail = async (row?: StationEnergyRecord) => {
  //proxy?.$modal.msg('模块开发中...');
  // proxy?.$tab.openPage('/station/stationMessage/info', '', { id: row?.stationId })
  recordInfoRef?.value.openEdit(row);
};
/** 删除按钮操作 */
const handleDelete = async (row?: StationEnergyRecord) => {
  const _ids = row?.stationId || '';
  await proxy?.$modal.confirm('是否确认该条数据项？');
  loading.value = true;
  await delEnergyRecord(_ids).finally(() => (loading.value = false));
  await getList();
  proxy?.$modal.msgSuccess('删除成功');
};

onMounted(() => {
  getList();
});
</script>
